<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: admin_misc.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/parser/functions_misc.php");
require_once ("./includes/functions_html.php");
$template->loadLanguageSection("mailparser_misc");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing Email Bans
*/
function _parserBanFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);
	$record["staffid"] = $_SWIFT["staffcache"][$record["staffid"]]["fullname"];

	$record["email"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="1"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .'icon_parserban.gif" border="0" /></td><td><span class="smalltext">&nbsp;<a href="index.php?_m=parser&_a=editemailban&parserbanid='. $record["parserbanid"] .'">'. htmlspecialchars($record["email"]) .'</a></span></td></tr></table>';

	$record["options"] = '<a href="index.php?_m=parser&_a=editemailban&parserbanid='. $record["parserbanid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["bandelconfirmmsg"] .'\', \'index.php?_m=parser&_a=emailbans&do=delete&parserbanid='. $record["parserbanid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Callback for parsing Catch-All Rules
*/
function _catchAllRuleFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);
	$record["emailqueueid"] = htmlspecialchars($_SWIFT["queuecache"]["list"][$record["emailqueueid"]]["email"]);
	$record["ruleexpr"] = htmlspecialchars($record["ruleexpr"]);

	$record["title"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="1"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .'icon_catchallrule.gif" border="0" /></td><td><span class="smalltext">&nbsp;<a href="index.php?_m=parser&_a=editcatchallrule&catchallruleid='. $record["catchallruleid"] .'">'. htmlspecialchars($record["title"]) .'</a></span></td></tr></table>';

	$record["options"] = '<a href="index.php?_m=parser&_a=editcatchallrule&catchallruleid='. $record["catchallruleid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["cruledelconfirmmsg"] .'\', \'index.php?_m=parser&_a=catchallrules&do=delete&catchallruleid='. $record["catchallruleid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Parser Bans
*/
function _maDeleteParserBans()
{
	global $dbCore, $_SWIFT, $infomessage;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (_is_array($_POST["itemid"]))
	{
		$infomessage = $_SWIFT["language"]["parserbansdelconfirm"];
		deleteParserBans($_POST["itemid"]);
	}
}

/**
* Mass Action: Delete Catch All Rules
*/
function _maDeleteCatchAllRules()
{
	global $dbCore, $_SWIFT, $infomessage;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (_is_array($_POST["itemid"]))
	{
		$infomessage = $_SWIFT["language"]["catchallruledelconfirm"];
		deleteCatchAllRules($_POST["itemid"]);
	}
}








/**
* ###############################################
* Email Bans
* ###############################################
*/
if ($eventaction == "emailbans")
{
	if ($_GET["do"] == "delete" && $_GET["parserbanid"] != "" && $_SWIFT["isdemo"] != true)
	{
		$infomessage = $_SWIFT["language"]["parserbansdelconfirm"];
		deleteParserBans(array($_GET["parserbanid"]));
	} else if ($_POST["insertban"] == 1 && $_SWIFT["isdemo"] != true) {
		if (trim($_POST["email"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			insertParserBan(array($_POST["email"]));

			printRedirect(sprintf($_SWIFT["language"]["parserbaninsertconfirm"], htmlspecialchars($_POST["email"])), "index.php?_m=parser&_a=emailbans&insertban=".urlencode($_POST["email"]));
			exit;
		}
	}

	if (trim($_REQUEST["insertban"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["parserbaninsertconfirm"], htmlspecialchars($_REQUEST["insertban"]));
	} else if (trim($_REQUEST["updateban"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["parserbanupdconfirm"], htmlspecialchars($_REQUEST["updateban"]));
	}


	$options["recordsperpage"] = "10";
	$options["sortby"] = "dateline";
	$options["sortorder"] = "desc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteParserBans";

	$options["idname"] = "parserbanid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["bannedemail"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'parserbans` WHERE `email` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_parserBanFields";
	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insertban"];
	$options["toolbar"][0]["url"] = "javascript:switchDisplay('insertban');";

	$fields[0]["name"] = "email";
	$fields[0]["title"] = $_SWIFT["language"]["bannedemail"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "staffid";
	$fields[1]["title"] = $_SWIFT["language"]["bannedby"];
	$fields[1]["width"] = "250";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "dateline";
	$fields[2]["title"] = $_SWIFT["language"]["date"];
	$fields[2]["width"] = "200";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "120";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["mailparser"]." > ".$_SWIFT["language"]["emailbans"], 1);

	$interface->adminNavBar('<a href="index.php?_m=parser&_a=manageemailqueues" title="'.$_SWIFT["language"]["mailparser"].'">'.$_SWIFT["language"]["mailparser"].'</a> &raquo; <a href="index.php?_m=parser&_a=emailbans" title="'.$_SWIFT["language"]["emailbans"].'">'.$_SWIFT["language"]["emailbans"].'</a>', $_SWIFT["language"]["desc_mailbans"] , 4);

	// ======= INSERT PARSER BAN =======
	echo '<div id="insertban" style="DISPLAY:none;">'.SWIFT_CRLF;
	renderParserBanForm(INSERT);
	echo '<BR /></div>'.SWIFT_CRLF;

	$grid->start("mailbans", $_SWIFT["language"]["emailbanlist"], 'SELECT * FROM `'. TABLE_PREFIX .'parserbans` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'parserbans`;', 'SELECT * FROM `'. TABLE_PREFIX .'parserbans` WHERE `email` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->display("mailbans");

	$template->assign("backurl", "index.php?_m=parser&_a=manageemailqueues");
	
	$interface->adminFooter();




/**
* ###############################################
* EDIT EMAIL BAN
* ###############################################
*/
} else if ($eventaction == "editemailban") {
	$_emailban = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."parserbans` WHERE `parserbanid` = '". intval($_REQUEST["parserbanid"]) ."';");
	if (empty($_emailban["parserbanid"]))
	{
		trigger_error($_SWIFT["language"]["invalidemailban"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1) {
		if (trim($_POST["email"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateParserBan($_emailban["parserbanid"], $_POST["email"]);

			printRedirect(sprintf($_SWIFT["language"]["parserbanupdconfirm"], htmlspecialchars($_POST["email"])), "index.php?_m=parser&_a=emailbans&updateban=".urlencode($_POST["email"]));
			exit;
		}
	}
	$_POST = array_merge($_POST, $_emailban);

	$interface->adminHeader($_SWIFT["language"]["mailparser"]." > ".$_SWIFT["language"]["editban"], 1);

	$interface->adminNavBar('<a href="index.php?_m=parser&_a=emailbans" title="'.$_SWIFT["language"]["emailbans"].'">'.$_SWIFT["language"]["emailbans"].'</a> &raquo; <a href="index.php?_m=parser&_a=editemailban&parserbanid='. intval($_emailban["parserbanid"]) .'" title="'.$_SWIFT["language"]["editban"].'">'.$_SWIFT["language"]["editban"].'</a>', $_SWIFT["language"]["desc_mailbans"] , 4);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderParserBanForm(EDIT);

	$template->assign("backurl", "index.php?_m=parser&_a=emailbans");

	$interface->adminFooter();




/**
* ###############################################
* MANAGE CATCH-ALL RULES
* ###############################################
*/
} else if ($eventaction == "catchallrules") {
	if ($_GET["do"] == "delete" && $_GET["catchallruleid"] != "" && $_SWIFT["isdemo"] != true)
	{
		$infomessage = $_SWIFT["language"]["catchallruledelconfirm"];
		deleteCatchAllRules(array($_GET["catchallruleid"]));
	} else if ($_POST["insertrule"] == 1 && $_SWIFT["isdemo"] != true) {
		if (trim($_POST["title"]) == "" || trim($_POST["ruleexpr"]) == "" || trim($_POST["emailqueueid"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			insertCatchAllRule($_POST["title"], $_POST["ruleexpr"], $_POST["emailqueueid"], $_POST["sortorder"]);

			printRedirect(sprintf($_SWIFT["language"]["catchruleinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=parser&_a=catchallrules&insertrule=".urlencode($_POST["title"]));
			exit;
		}
	}

	if (trim($_REQUEST["insertrule"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["catchruleinsertconfirm"], htmlspecialchars($_REQUEST["insertrule"]));
	} else if (trim($_REQUEST["updaterule"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["catchruleupdconfirm"], htmlspecialchars($_REQUEST["updaterule"]));
	}

	$options["recordsperpage"] = "10";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteCatchAllRules";

	$options["idname"] = "catchallruleid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'catchallrules` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["rregexp"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'catchallrules` WHERE `ruleexpr` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_catchAllRuleFields";
	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insertnewrule"];
	$options["toolbar"][0]["url"] = "javascript:switchDisplay('insertrule');";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["title"];
	$fields[0]["width"] = "150";

	$fields[1]["name"] = "ruleexpr";
	$fields[1]["title"] = $_SWIFT["language"]["rregexp"];
	$fields[1]["width"] = "";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "emailqueueid";
	$fields[2]["title"] = $_SWIFT["language"]["emailqueue"];
	$fields[2]["width"] = "180";
	$fields[2]["align"] = "center";

	$fields[3]["name"] = "dateline";
	$fields[3]["title"] = $_SWIFT["language"]["date"];
	$fields[3]["width"] = "200";
	$fields[3]["align"] = "center";

	$fields[4]["type"] = "custom";
	$fields[4]["name"] = "options";
	$fields[4]["title"] = $_SWIFT["language"]["options"];
	$fields[4]["width"] = "120";
	$fields[4]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["mailparser"]." > ".$_SWIFT["language"]["catchallrules"], 1);

	$interface->adminNavBar('<a href="index.php?_m=parser&_a=manageemailqueues" title="'.$_SWIFT["language"]["mailparser"].'">'.$_SWIFT["language"]["mailparser"].'</a> &raquo; <a href="index.php?_m=parser&_a=catchallrules" title="'.$_SWIFT["language"]["catchallrules"].'">'.$_SWIFT["language"]["catchallrules"].'</a>', $_SWIFT["language"]["desc_catchallrules"] , 4);

	// ======= INSERT NEW RULE =======
	echo '<div id="insertrule" style="DISPLAY:block;">'.SWIFT_CRLF;
	renderCatchAllRuleForm(INSERT);
	echo '<BR /></div>'.SWIFT_CRLF;

	$grid->start("catchallrules", $_SWIFT["language"]["catchallrules"], 'SELECT * FROM `'. TABLE_PREFIX .'catchallrules` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'catchallrules`;', 'SELECT * FROM `'. TABLE_PREFIX .'catchallrules` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->display("catchallrules");

	$template->assign("backurl", "index.php?_m=parser&_a=manageemailqueues");
	
	$interface->adminFooter();







/**
* ###############################################
* EDIT CATCH-ALL RULE
* ###############################################
*/
} else if ($eventaction == "editcatchallrule") {
	$_catchallrule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."catchallrules` WHERE `catchallruleid` = '". intval($_REQUEST["catchallruleid"]) ."';");
	if (empty($_catchallrule["catchallruleid"]))
	{
		trigger_error($_SWIFT["language"]["invalidcatchalrule"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1) {
		if (trim($_POST["title"]) == "" || trim($_POST["ruleexpr"]) == "" || trim($_POST["emailqueueid"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateCatchAllRule($_catchallrule["catchallruleid"], $_POST["title"], $_POST["ruleexpr"], $_POST["emailqueueid"], $_POST["sortorder"]);

			printRedirect(sprintf($_SWIFT["language"]["catchruleupdconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=parser&_a=catchallrules&updaterule=".urlencode($_POST["title"]));
			exit;
		}
	}
	$_POST = array_merge($_POST, $_catchallrule);

	$interface->adminHeader($_SWIFT["language"]["mailparser"]." > ".$_SWIFT["language"]["editcatchallrule"], 1);

	$interface->adminNavBar('<a href="index.php?_m=parser&_a=catchallrules" title="'.$_SWIFT["language"]["catchallrules"].'">'.$_SWIFT["language"]["catchallrules"].'</a> &raquo; <a href="index.php?_m=parser&_a=editcatchallrule&catchallruleid='. intval($_catchallrule["catchallruleid"]) .'" title="'.$_SWIFT["language"]["editcatchallrule"].'">'.$_SWIFT["language"]["editcatchallrule"].'</a>', $_SWIFT["language"]["desc_catchallrules"] , 4);


	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCatchAllRuleForm(EDIT);

	$template->assign("backurl", "index.php?_m=parser&_a=catchallrules");

	$interface->adminFooter();




/**
* ###############################################
* PARSE RAW EMAIL
* ###############################################
*/
} else if ($eventaction == "parseemail") {
	
}




?>